#
# PolarDB 15.12 with TimescaleDB 2.21.0
#
# docker image build --no-cache --tag polardb/polardb_pg:15.12-tsdb ./
# docker image build --no-cache --build-arg TSDB_VERSION=2.21.1 --tag polardb/polardb_pg:15.12-tsdb ./
#
# docker image rm polardb/polardb_pg:15.12-tsdb
#
# docker image save -o polardb-15.12-tsdb.tar polardb/polardb_pg:15.12-tsdb
# docker image load -i polardb-15.12-tsdb.tar
#
ARG POLARDB_PG_VERSION=15.12.3.0.f5e74938

FROM registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_local_instance:${POLARDB_PG_VERSION} AS builder

ARG POLARDB_PG_VERSION=15.12.3.0.f5e74938
ARG TSDB_VERSION=2.21.0

USER root

RUN curl -fLSs https://github.com/timescale/timescaledb/archive/refs/tags/${TSDB_VERSION}.tar.gz | tar --directory=/usr/local/src/ -xz; \
    cd /usr/local/src/timescaledb-${TSDB_VERSION}; \
    ./bootstrap -DUSE_OPENSSL=0 -DREGRESS_CHECKS=OFF; \
    cd ./build && make; \
    make install


FROM registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_local_instance:${POLARDB_PG_VERSION}

ARG POLARDB_PG_VERSION=15.12.3.0.f5e74938
ARG TSDB_VERSION=2.21.0

LABEL name="PolarDB with TimescaleDB" \
      vendor="liym ijliym@163.com" \
      maintainer="liym ijliym@163.com" \
      version="PolarDB ${POLARDB_PG_VERSION}" \
      release="TimescaleDB ${TSDB_VERSION}" \
      summary="PolarDB 15.12 with TimescaleDB ${TSDB_VERSION}" \
      description="PolarDB 15.12 with TimescaleDB ${TSDB_VERSION}"

USER root

COPY --from=builder /u01/polardb_pg/share/postgresql/extension/timescaledb* /u01/polardb_pg/share/postgresql/extension/
COPY --from=builder /u01/polardb_pg/lib/postgresql/timescaledb* /u01/polardb_pg/lib/postgresql/

RUN chown -R postgres:postgres /u01/polardb_pg/share/postgresql/extension/timescaledb*; \
    chown -R postgres:postgres /u01/polardb_pg/lib/postgresql/timescaledb*; \
    sed -r -i "s/[#]*\s*(shared_preload_libraries)\s*=\s*'(.*)'/\1 = 'timescaledb,\2'/;s/,'/'/" /u01/polardb_pg/share/postgresql/polardb.conf.sample; \
    apt-get autoremove -y; \
    apt-get purge --auto-remove -o APT::AutoRemove::RecommendsImportant=false -y; \
    apt-get clean all; \
    rm -rf /var/lib/apt/lists/*

